layui.use(['form', 'table', 'layer', 'jquery'], function () {
    var table = layui.table,
        layer = layui.layer,
        $ = layui.jquery;


    var tableIns = table.render({
        elem: '#deptList', //绑定table id
        height: 'full-125',
        url: ctx + '/dept/empDept',
        page: true,
        cellMinWidth: 95, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[ // 表头
            {type: 'checkbox'},
            {type: "numbers", title: '编号', width: 150}
            , {field: 'deptName', title: '部门名称', width: 150 ,templet :function (data){
                return data.deptBean.deptName
                }}
            , {
                title: '部门负责人', width: 150, templet: function (data) {
                    if (data.empBean ==  null) {
                        return  '' ;
                    }
                    return data.empBean.empName
                }
            }
            , {field: 'deptPosition', title: '部门表述', width: 150 ,templet:function (data){
                return data.deptBean.deptPosition
                }}
            , {title: '操作', templet: '#currentTableBar', fixed: 'right', minWidth: '150'}
        ]]
    });

    function openDeleteDeptDialog(id) {
        layer.confirm("确认删除选中的记录吗", {icon: 3, title: "部门调整"}, function (index) {
            layer.close(index)
            $.ajax({
                type: "post",
                url: ctx + "/dept/delete",
                data: {
                    id: id
                },
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg("删除成功", {icon: 6})
                        tableIns.reload()
                    } else {
                        layer.msg(result.msg, {icon: 5})
                    }
                }
            })
        })
    }

    // 监听 行  工具栏
    table.on('tool(deptFilter)', function (data) {
        if (data.event == 'edit') {
            openAddOrUpdateDeptDialog(data.data);
        } else if (data.event == 'delete') {
            openDeleteDeptDialog(data.data.id);
        }
    })

    // 监听头部工具栏
    table.on('toolbar(deptFilter)', function (data) {
        console.log(data)
        if (data.event == 'add') {
            openAddOrUpdateDeptDialog();
        }


    })

    function openAddOrUpdateDeptDialog(data) {

        var title = "<h3>组织架构 ----  添加部门</h3>";
        var url = ctx + "/dept/toAddOrUpdateDeptPage";

        if (data.id != null && data.id != '') {
            var title = "<h3>组织架构 ----  修改部门</h3>";
            url = ctx + "/dept/toAddOrUpdateDeptPage?deptId=" + data.deptId;

        }
        layer.open({
            type: 2,
            title: title,
            area: ['700px', '500px'],
            content: url,
            shadeClose: true,
            maxmin: true
        })
    }
})